#! /usr/bin/python
#  -*- coding: utf-8 -*-
import time

from BeautifulSoup import BeautifulSoup


from libs import htmlparser

from libs.functions import *


import urllib2
import re
class dataChecker:
 
 
    '''
    判断url类型
    '''
    def __init__(self,ini,iniXml):
        self.urlRule=[]
 
        for i in  ini :
           
            self.urlRule.append({'list':re.compile(str(i['list'])),'dom':re.compile(str(i['dom'])),'article':re.compile(str(i['article']))})
            
#         
#            
        for i in  iniXml :
            self.iniXml=re.compile(str(i))
    
    
    
    def _checkType(self,url,types):
        for res  in self.urlRule:
            if res[types].findall(url):
                return True
        
        return False
    
    
    def checkList(self,url):
        
        return self._checkType(url, 'list')
    
    
    def checkArticle(self,url):
        
        return self._checkType(url, 'article')
    def checkDom(self,url):
        return self._checkType(url,'dom')
    
    def checkXml(self,url):
        for res  in self.iniXml:
            if res.findall(url):
                return True
        
        return False
        







class urlAdminer:
    '''
    url 控制 ，取得存入url 
    '''
    def __init__(self, db,urlcheck):
        self.db = db
        self.urlcheck=urlcheck


    def getUrls(self,  urls='urls'):
        sql = "select urls ,id from %s order by nums asc ,id asc limit 1" % (urls)
        
        res = self.db.fetch(sql, 1)
        self.db.commit()
        r = []
        ids = []
        if res:
            
           
            t = str(int(time.time()))
            sql = "update %s set nums=%s where id = %s" % (urls,t, res['id'])
            self.db.execsql(sql)
            self.db.commit()
            
         
            
            return str (res['urls'])
        else:
            return 'http://meishichina.com/'


    def setUrls(self, urls):
        
        if self.urlcheck.checkArticle(urls[0]):
            tb='article'
        elif self.urlcheck.checkDom(urls[0]):
            tb='urls'
        elif self.urlcheck.checkXml(urls[0]):
            self.setXmls(urls[0])
            return  True
            
        print 'sucess inserted %s'%urls
        sql = "insert into %s (urls) values ('%s')" % (tb, urls[0])

        self.db.execsql(sql)
        self.db.commit()
        
        
    def setXmls(self, urls):

        sql = "insert into xmls (urls) values ('%s')" % urls[0]
        self.db.execsql(sql)
        self.db.commit()
        


        